home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacTCPToolBx / Source Code ƒ / splitByWord.p < prev    next >
Text File  |  1989-06-01  |  4KB  |  143 lines

  1. (*
  2.     splitByWord(string,firstWord,lastWord) - Take a string containing a number of lines, and return a string
  3.         with the same number of lines, each one containing word firstWord through word lastWord of the original
  4.         line.
  5.  
  6.     To compile and link this file using Macintosh Programmers Workshop,
  7.  
  8.         pascal -w splitByWord.p
  9.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=7871 -sn Main=splitByWord ∂
  10.             splitByWord.p.o "{Libraries}HyperXLib.o" "{MPW}"Libraries:interface.o "{MPW}"Libraries:PLibraries:PasLib.o
  11.  
  12.     © Copyright 1989 by Apple Computer, Inc.
  13.  
  14.     Initial coding 3/24/89 by Harry R. Chesley.
  15. *)
  16.  
  17. {$R-}
  18.  
  19. {$S splitByWord }     { Segment name must be the same as the command name. }
  20.  
  21. unit DummyUnit;
  22.  
  23. interface
  24.  
  25. uses MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;
  26.  
  27. procedure EntryPoint(paramPtr: XCmdPtr);
  28.     
  29. implementation
  30.  
  31. const
  32.  
  33. kReturn = 13;                { ASCII for carriage return. }
  34.  
  35. procedure splitByWord(paramPtr: XCmdPtr); forward;
  36.  
  37. procedure EntryPoint(paramPtr: XCmdPtr);
  38.  
  39.     begin
  40.         splitByWord(paramPtr);
  41.     end;
  42.  
  43. procedure splitByWord(paramPtr: XCmdPtr);
  44.  
  45.     var str: Str255;
  46.         sourceHand: Handle;                { Handle to the original source text. }
  47.         firstWord: integer;                { Which word to start with. }
  48.         lastWord: integer;                { Which word to end with. }
  49.         resultHand: Handle;                { Handle to the result text. }
  50.         resultPtr: Ptr;
  51.         resultSize: longInt;                { Size of the result. }
  52.  
  53.     procedure Fail(errMsg: Str255); { set theResult and quit }
  54.         begin
  55.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  56.             exit(splitByWord);
  57.         end;
  58.  
  59.     procedure processSource(procedure makeLine(linePtr: Ptr; lineSize: longInt));
  60.         { Run through the input and call makeLine for each line in it. }
  61.  
  62.         var p: Ptr;                { Input pointer. }
  63.             copyStart: Ptr;    { Pointer to beginning of section to copy. }
  64.             i: integer;
  65.  
  66.         begin
  67.             { Start at the start. }
  68.             p := sourceHand^;
  69.             while p^ <> 0 do
  70.                 begin
  71.                     { Find the beginning of the first word to copy. }
  72.                     i := 1;
  73.                     while i < firstWord do
  74.                         begin
  75.                             while p^ = ord(' ') do p := Ptr(ord4(p)+1);
  76.                             while (p^ <> ord(' ')) and (p^ <> kReturn) and (p^ <> 0) do p := Ptr(ord4(p)+1);
  77.                             i := i+1;
  78.                         end;
  79.                     while p^ = ord(' ') do p := Ptr(ord4(p)+1);
  80.                     copyStart := p;
  81.                     { Find the end of the last word to copy. }
  82.                     while i <= lastWord do
  83.                         begin
  84.                             while p^ = ord(' ') do p := Ptr(ord4(p)+1);
  85.                             while (p^ <> ord(' ')) and (p^ <> kReturn) and (p^ <> 0) do p := Ptr(ord4(p)+1);
  86.                             i := i+1;
  87.                         end;
  88.                     { Call makeLine with the part of the line to be copied. }
  89.                     makeLine(copyStart,ord4(p)-ord4(copyStart));
  90.                     { Skip to the end of the line. }
  91.                     while (p^ <> kReturn) and (p^ <> 0) do p := Ptr(ord4(p)+1);
  92.                     if p^ = kReturn then p := Ptr(ord4(p)+1);
  93.                 end;
  94.         end;
  95.  
  96.     procedure countSizes(linePtr: Ptr; lineSize: longInt);
  97.         { Routine to pass to processInput to figure the size of the output. }
  98.  
  99.         begin
  100.             resultSize := resultSize + lineSize + 1;
  101.         end;
  102.  
  103.     procedure storeLine(linePtr: Ptr; lineSize: longInt);
  104.         { ROutine to pass to processInput to copy the result into the output. }
  105.  
  106.         begin
  107.             BlockMove(linePtr,resultPtr,lineSize);
  108.             resultPtr := Ptr(ord4(resultPtr)+lineSize);
  109.             resultPtr^ := kReturn;
  110.             resultPtr := Ptr(ord4(resultPtr)+1);
  111.         end;
  112.  
  113.     begin
  114.         if paramPtr^.paramCount <> 3 then Fail('§§§ parameter count is not 3 §§§');
  115.  
  116.         sourceHand := paramPtr^.params[1];                        { First parameter is the string to be grouped. }
  117.         ZeroToPas(paramPtr,paramPtr^.params[2]^,str);        { Second parameter is the number of the first word to copy. }
  118.         firstWord := StrToNum(paramPtr,str);
  119.         if firstWord < 1 then firstWord := 1;
  120.         ZeroToPas(paramPtr,paramPtr^.params[3]^,str);        { Third parameter is the number of the last word to copy. }
  121.         lastWord := StrToNum(paramPtr,str);
  122.  
  123.         { If there's anything in the string, process it. }
  124.         if sourceHand <> NIL then
  125.             begin
  126.                 { Figure out the size of the result (including the null termination). }
  127.                 resultSize := 1;
  128.                 processSource(countSizes);
  129.                 if resultSize = 1 then Fail('');
  130.                 { Allocate the result handle. }
  131.                 resultHand := NewHandle(resultSize);
  132.                 if memError <> noErr then Fail('§§§ could not allocate result §§§');
  133.                 { Copy in the data. }
  134.                 resultPtr := resultHand^;
  135.                 processSource(storeLine);
  136.                 resultPtr^ := 0;
  137.                 paramPtr^.returnValue := resultHand;
  138.             end
  139.         else Fail('');
  140.     end;
  141.  
  142. end.
  143.